<?php
	namespace LEA\UserBundle\Utility;

	use Symfony\Component\HttpFoundation\Session;
	use LEA\UserBundle\SQL\other\queriesAuth;
	use LEA\UserBundle\Utility\Utility;

	class Auth {

		function checkUserRole($request, $conn){
			$session = $request->getSession();
	        $session->set('CK_ROLES', "") ;
	        $utils = new Utility();

	        $user=$utils->getParam('CK_USER',$session->get('CK_USER'), $request);;

	        error_log("checking roles for ".$user);

	        $queriesAuth = new QueriesAuth($conn);

	        if($queriesAuth->doCheckStudentRole($user)){
		       $session->set('CK_ROLES', $session->get('CK_ROLES')."STUD_ROLE") ;
	        }else{
		        if($queriesAuth->doCheckProfRole($user)){
			         $session->set('CK_ROLES', $session->get('CK_ROLES')."PROF_ROLE") ;
		        }

		        if($queriesAuth->doCheckSecrRole($user)){
			        $session->set('CK_ROLES', $session->get('CK_ROLES')."SECR_ROLE") ;
			    }

	        }

	        $queriesAuth->checkRoles($user, $session);

	        error_log("found roles ".$session->get('CK_ROLES'));
	    }

	    function hasRole($request, $role)
	    {
	    	$utils = new Utility();
	    	$session = $request->getSession();

	        $user=$utils->getParam('CK_USER',$session->get('CK_USER'), $request);
	        $roles=$utils->getParam('CK_ROLES',$session->get('CK_ROLES'), $request);

	        error_log("requires role ".$role." for ".$user. " | ". $roles);
	        if ($user===null || $roles === null)
	        {
	            return FALSE;
	        }

	        if (strpos($roles,"ROLEALL",0)!== FALSE){
	        	return TRUE;
	        }

			if($role != "RESP_ROLE"){
				if (strlen($role)==0)
				  return TRUE;
			}
	        //error_log("cookie exist : ".$roles);

	        return (strpos($roles,$role,0)!== FALSE);
	    }
	}
?>